﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Speedy
{
    public partial class frmStokPromosyon : XtraForm
    {
        public frmStokPromosyon()
        {
            InitializeComponent();
        }

        private void buttonEdit3_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmListe lst = new frmListe();
            lst.ListeTipi = ListeTipleri.CariKodu;
            lst.Filtre = txtCari.Text;
            if (lst.ShowDialog() == DialogResult.OK)
                if (lst.Value.Length > 0)
                {
                    lblCariID.Text = lst.Value[0];
                    txtCari.Text = lst.Value[1];
                }
        }

        private void frmStokPromosyon_Load(object sender, EventArgs e)
        {
            dbPromotionHeader prm = new dbPromotionHeader();
            prm.code.Alias = "CODE";
            prm.name.Alias = "NAME";
            DataTable dt = prm.AllData();
            cmbKamp.ValueMember = "CODE";
            cmbKamp.DisplayMember = "NAME";
            cmbKamp.DataSource = dt;

            Liste();
        }

        private void Liste()
        {
            dbProductPromotion p = new dbProductPromotion();
            gridControl1.DataSource = p.AllData();
            gridView1.Columns["PrmStok"].Visible = false;
            gridView1.Columns["PROMOTION_ID"].Visible = false;
            gridView1.Columns["PRODUCT_ID"].Visible = false;
            gridView1.Columns["PRM_PRODUCT_ID"].Visible = false;
            gridView1.Columns["CUSTOMER_ID"].Visible = false;
            gridView1.Columns["cari_kod"].Visible = false;
            gridView1.Columns["sto_kod"].Visible = false;
        }

        private void txtStok_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmListe lst = new frmListe();
            lst.ListeTipi = ListeTipleri.StokKodu;
            lst.Filtre = txtStok.Text;
            if (lst.ShowDialog() == DialogResult.OK)
                if (lst.Value.Length > 0)
                {
                    lblStokID.Text = lst.Value[0];
                    txtStok.Text = lst.Value[1];
                }
        }

        private void txtKampStok_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmListe lst = new frmListe();
            lst.ListeTipi = ListeTipleri.StokKodu;
            lst.Filtre = txtKampStok.Text;
            if (lst.ShowDialog() == DialogResult.OK)
                if (lst.Value.Length > 0)
                {
                    lblPrmStokID.Text = lst.Value[0];
                    txtKampStok.Text = lst.Value[1];
                }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            dbProductPromotion prm = new dbProductPromotion();
            prm.customerId.SetValue(lblCariID.Text);
            prm.productId.SetValue(lblStokID.Text);
            prm.promoId.SetValue(cmbKamp.SelectedValue);
            prm.promoProductId.SetValue(lblPrmStokID.Text);
            prm.Save();
            Liste();
        }


        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(e.FocusedRowHandle);
        }

        void SelectRecord(int row)
        {
            txtStok.Text = gridView1.GetRowCellValue(row, gridView1.Columns["sto_kod"]).ToString();
            cmbKamp.SelectedValue = gridView1.GetRowCellValue(row, gridView1.Columns["PROMOTION_ID"]);
            txtKampStok.Text = gridView1.GetRowCellValue(row, gridView1.Columns["PrmStok"]).ToString();
            txtCari.Text = gridView1.GetRowCellValue(row, gridView1.Columns["cari_kod"]).ToString();

            lblCariID.Text = gridView1.GetRowCellValue(row, gridView1.Columns["CUSTOMER_ID"]).ToString();
            lblPrmStokID.Text = gridView1.GetRowCellValue(row, gridView1.Columns["PRM_PRODUCT_ID"]).ToString();
            lblStokID.Text = gridView1.GetRowCellValue(row, gridView1.Columns["PRODUCT_ID"]).ToString();
            cmbKamp.SelectedValue = gridView1.GetRowCellValue(row, gridView1.Columns["PROMOTION_ID"]);

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            dbProductPromotion prm = new dbProductPromotion();
            prm.productId.SetValue(lblStokID.Text);
            prm.promoId.SetValue(cmbKamp.SelectedValue);
            prm.Remove();
            Liste();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(gridView1.FocusedRowHandle);
        }


    }
}
